package org.yds.quality.function;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType;
import org.yds.quality.dto.DefectDTO;

import java.util.HashMap;
import java.util.Map;


public class CheckFunction extends AbstractFunction {
    @Override
    public AviatorObject call(Map<String, Object> env, AviatorObject countObj, AviatorObject conditionObj) {
        // 获取参数值
        long count = (long) countObj.getValue(env);
        String condition = (String) conditionObj.getValue(env);

        // 执行逻辑表达式
        Map<String, Object> localEnv = new HashMap<>(env);
        localEnv.put("count", count);
        boolean success = (boolean) AviatorEvaluator.execute(condition, localEnv);
        // 返回自定义对象
        return new AviatorRuntimeJavaType(new DefectDTO(success, count));
    }

    @Override
    public String getName() {
        return "Check";
    }
}
